package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.protocol.IDynamicCacheService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDataType;
import com.ruoyi.system.service.ISysDataTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字段类型Controller
 * 
 * @author ruoyi
 * @date 2021-10-31
 */
@RestController
@RequestMapping("/system/dataType")
public class SysDataTypeController extends BaseController
{
    @Autowired
    private ISysDataTypeService sysDataTypeService;

    @Autowired
    private IDynamicCacheService dynamicCache;

    /**
     * 查询字段类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDataType sysDataType)
    {
        startPage();
        List<SysDataType> list = sysDataTypeService.selectSysDataTypeList(sysDataType);
        return getDataTable(list);
    }

    /**
     * 导出字段类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:export')")
    @Log(title = "字段类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDataType sysDataType)
    {
        List<SysDataType> list = sysDataTypeService.selectSysDataTypeList(sysDataType);
        ExcelUtil<SysDataType> util = new ExcelUtil<SysDataType>(SysDataType.class);
        return util.exportExcel(list, "字段类型数据");
    }

    /**
     * 获取字段类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDataTypeService.selectSysDataTypeById(id));
    }

    /**
     * 新增字段类型
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:add')")
    @Log(title = "字段类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDataType sysDataType)
    {
        int i = sysDataTypeService.insertSysDataType(sysDataType);
        dynamicCache.refreshDataTypeListByCache();
        dynamicCache.refreshFieldForwardAndReverse();
        return toAjax(i);
    }

    /**
     * 修改字段类型
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:edit')")
    @Log(title = "字段类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDataType sysDataType)
    {
        int i = sysDataTypeService.updateSysDataType(sysDataType);
        dynamicCache.refreshDataTypeListByCache();
        dynamicCache.refreshFieldForwardAndReverse();
        return toAjax(i);
    }

    /**
     * 删除字段类型
     */
    @PreAuthorize("@ss.hasPermi('system:dataType:remove')")
    @Log(title = "字段类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = sysDataTypeService.deleteSysDataTypeByIds(ids);
        dynamicCache.refreshDataTypeListByCache();
        dynamicCache.refreshFieldForwardAndReverse();
        return toAjax(i);
    }
}
